﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PaymentHelper.Core.ViewEmbeddedHandler
{
    [Serializable]
    public class EmbeddedViewCollection
    {
        private readonly object _object = new object();
        private readonly Dictionary<string, EmbeddedViewData> _viewCache;

        public IList<EmbeddedViewData> Views
        {
            get { return _viewCache.Values.ToList(); }
        }

        public EmbeddedViewCollection()
        {   
            _viewCache = new Dictionary<string, EmbeddedViewData>();
        }

        public void AddView(string name,string assemblyFullName)
        {
            lock (_object)
            {
                _viewCache[name] = new EmbeddedViewData() { ViewName = name, AssemblyFullName = assemblyFullName };
            }
        }

        public EmbeddedViewData FindView(string name)
        {
            if (string.IsNullOrEmpty(name))
                return null;

            return Views.Where(v => v.ViewName.ToLowerInvariant().Equals(name.ToLowerInvariant())).FirstOrDefault();
        }

        public bool ContainsView(string name)
        {
            return FindView(name) != null;
        }

    }

    [Serializable]
    public class EmbeddedViewData
    {
        public string ViewName { get; set; }
        public string AssemblyFullName { get; set; }
    }
}
